package cc.messcat.web.fupin;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.constant.CommonRedisConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.entity.fupin.Dequeue;
import cc.messcat.entity.fupin.PoorVillage;
import cc.messcat.entity.fupin.PoorVillageHistory;
import cc.messcat.entity.system.ApplicationProperties;
import cc.messcat.entity.system.McUser;
import cc.messcat.log.OperationLog;
import cc.messcat.service.fupin.DequeueService;
import cc.messcat.service.fupin.PoorVillageService;
import cc.messcat.service.system.UserService;

@Controller
@RequestMapping("/dequCon")
public class DequeueController {

	@Autowired
	private DequeueService dequeueService;
	
	@Autowired
	private PoorVillageService poorVillageService;
	
    @Autowired
    public UserService userService;
	
	@RequestMapping("/addDequeue")
	@ResponseBody
	public Object addDequeue(@Valid Dequeue dequeue,Long pid,HttpSession session){
		PoorVillage poorVillage = this.poorVillageService.get(pid);
		dequeue.setAddTime(new Date());
		dequeue.setReTime(new Date());
		dequeue.setAnnual(ApplicationProperties.getAnnual());
		dequeue.setOriginPoorVillage(poorVillage);
		if(poorVillage != null){
			PoorVillageHistory poorVillageHistory = new PoorVillageHistory();
			BeanUtils.copyProperties(poorVillage, poorVillageHistory);
			poorVillageHistory.setId(null);
			poorVillageHistory.setOriginId(pid);
			dequeue.setPoorVillageHistory(poorVillageHistory);
			poorVillageService.addPoorVillageHistory(poorVillageHistory);
			dequeueService.addObject(dequeue);
			
			if("0".equals(poorVillage.getStatus())){
				poorVillage.setStatus("1");
			}else{
				poorVillage.setStatus("0");
			}
	      }
		
		poorVillageService.modifyPoorVillage(poorVillage);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_ADD_SUCCESS);
	}
	
    /**
     * 贫困村变更查询
     */	
	@OperationLog(desc="贫困村变更查询",module="帐表查询")
	@RequestMapping("/queryDequeue")
	@ResponseBody
	public Object queryDequeue(@Valid Dequeue dequeue,String nature,Integer pageNo, Integer pageSize,HttpServletRequest req,HttpServletRequest request,String cry,String name){
		 if (pageNo == null || pageNo < 1) {
	            pageNo = 1;
	        }
	        if (pageSize == null || pageSize < 1) {
	            pageSize = 10;
	        }
	        McUser user = (McUser)req.getSession().getAttribute("user");
	        String pid = request.getParameter("pid");
	        dequeue.setAnnual(request.getParameter("years"));
	       Pager pager = this.dequeueService.getPagertwo(dequeue,nature,pageNo, pageSize,user,pid,cry,name);
		
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,pager);
	}
	
	
	/**
	 * 贫困村变更查询导出
	 */
	@OperationLog(desc="贫困村变更查询导出",module="帐表导出")
	@RequestMapping("/exportqueryDequeue")
	@ResponseBody
	public Object exportqueryDequeue(@Valid Dequeue dequeue,String nature,HttpSession session,HttpServletResponse response,String pid,String cry,String name,HttpServletRequest request) {
		McUser user = userService.retrieveUserByUsername((String) session.getAttribute(CommonRedisConstant.REDIS_KEY_LOGIN_USER));
		try {
			dequeue.setAnnual(request.getParameter("years"));
			HSSFWorkbook wb = dequeueService.exportVisitRecd(dequeue,nature,user,pid,cry,name);
			
			response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("贫困村变更","UTF-8")+".xls");
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
			wb.write(bufferedOutputStream);
			bufferedOutputStream.close();
			
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXPORT_SUCCESS);
		} catch (IOException e) {
			e.printStackTrace();
			return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_EXPORT_FAIL);
		}
	}
	
	
	/**
	 * 根据id贫困村变更查询
	 */
	@RequestMapping("/queryDequeueid")
	@ResponseBody
	public Object queryDequeueid(Long id) {
		 List<Dequeue> dequeues = this.dequeueService.Dequeue(id);
		 return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,dequeues);
		}
}
